package com.lashou.labelutil.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.lashou.labelutil.dao.TaobaoDAO;
import com.lashou.labelutil.model.Deal;


public class TaobaoAction extends DispatchAction {
	private TaobaoDAO taobaoDAO;
	
	public TaobaoDAO getTaobaoDAO() {
		return taobaoDAO;
	}

	public void setTaobaoDAO(TaobaoDAO taobaoDAO) {
		this.taobaoDAO = taobaoDAO;
	}

	
	public ActionForward initPage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Deal deal = this.taobaoDAO.getDealByNaturalKey("taobao");
		request.setAttribute("deal", deal);
		request.setAttribute("total",this.taobaoDAO.getTaobaoTotal());
	
		
		return mapping.findForward("sucess");
	}
	
	
	public ActionForward updateDeal(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		try
		{
			//request.setCharacterEncoding("GBK");
			String dealId = request.getParameter("dealId").trim();
			if(dealId==null){
				return new ActionForward("/taobao.do?method=initPage"); 
			}
			
			String description = request.getParameter("description").trim();
			String commentsNumber = request.getParameter("commentsNumber").trim();//評價人數
			String commentsScore = request.getParameter("commentsScore").trim();//得分
			String companyName = request.getParameter("companyName").trim();//公司名	
			String businessName = request.getParameter("businessName").trim();//淘寶店鋪名
			String companyAddrees =request.getParameter("companyAddrees").trim();
			
			description = new String(description.getBytes("ISO-8859-1"),"UTF-8");
			companyName = new String(companyName.getBytes("ISO-8859-1"),"UTF-8");
			businessName = new String(businessName.getBytes("ISO-8859-1"),"UTF-8");
			companyAddrees = new String(companyAddrees.getBytes("ISO-8859-1"),"UTF-8");
			
			
			Deal deal = new Deal();
			deal.setId(new Integer(dealId));
			
			if(null!=commentsNumber&&!"".equals(commentsNumber))
			deal.setCommentsNumber(new Integer(commentsNumber));
			
			if(null!=commentsScore&&!"".equals(commentsScore))
			deal.setCommentsScore(new Double(commentsScore));
			deal.setCompanyName(companyName);			
			deal.setBusinessName(businessName);
			deal.setCompanyAddrees(companyAddrees);
			deal.setDescription(description);
			
			this.taobaoDAO.updateDealForTaobao(deal);
			//System.out.println(deal.getId()+" "+deal.getCommentsNumber()+" "+deal.getCommentsScore()+" "+deal.getCompanyName()+" "+deal.getBusinessName()+" "+deal.getCompanyAddrees());
		}catch(Exception e)
		{
			//e.printStackTrace();
		}
		return new ActionForward("/taobao.do?method=initPage");
	}

}
